<?php

class Default_ActivitesController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {
        // action body
        $mod = new Application_Model_ActivityModel();


        $this->view->nb = $mod->countAll();
        $this->view->tabActivity = $mod->readAll();
        $this->view->comments = $mod->getComments();


        $form = new Default_Form_Comment();

        $this->view->form = $form;

        // si le formulaire a été soumis
        if ($this->_request->isPost()) {
// on récupère les variables Post
            $formData = $this->_request->getPost();
// si les critères de validation sont respectés
            if ($form->isValid($formData)) {
                $id = $form->getValue('firstname');
                $author_firstname = $form->getValue('comment_firstname');
                $author_name = $form->getValue('comment_name');
                $content = $form->getValue('comment_content');


                $comment = new Application_Model_Comment();
                $comment->setId($id);
                $author = new Application_Model_People($author_name, $author_firstname);
                $comment->setAuthor($author);
                $comment->setContent($content);
                $comment->setDate(date("d/m/Y"));

                $mod->addComment($id, $comment);
//on nettoie les champs du formulaire .
                $form->reset();
            }
        }
    }

    public function readAction() {
        // action body
        $mod = new Application_Model_ActivityModel();
        $id = $this->getRequest()->getParam('id');
        $this->view->activity = $mod->read($id);
    }

    public function deletecommentAction() {
        $mod = new Application_Model_ActivityModel();
        $id = $this->getRequest()->getParam('id');
        $mod->removeComment($id);
        $this->_forward('index');
        // action body
    }

}

